home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / basic / UDP_Chat.lha / UDPChat / Old_Stuff / UDP_Send.asc < prev    next >
Encoding:
Text File  |  1998-08-02  |  2.9 KB  |  130 lines

  1. ;
  2. ;                      UDP Send code
  3. ;
  4. ;    This code sends each string to an address (port on a host).
  5. ;
  6. ;  Written by Anton Reinauer <anton@ww.co.nz.
  7. ;
  8. ;  Thanks to Paul Burkey for TCP_Funcs and to Dr. Ercole Spiteri
  9. ;  for TCP-to-Blitz.
  10. ;
  11. ;  Turn overflow errors off if running on a 68000
  12.  
  13.  
  14. WBStartup
  15. NoCli
  16. Hostname.s="localhost"  ; destination host address
  17. #PORT=3001              ; destination port to send data to
  18.  
  19. INCLUDE "TCPFuncs.bb"
  20. DEFTYPE .w
  21.  
  22. ;********************************************************
  23.  
  24. .WriteUDP
  25. Statement WriteUDP{ad.l,size.w,sendtolen.l}
  26.   SHARED sock.l,host.sockaddrin
  27.  
  28.   ; This routine writes data via UDP
  29.  
  30.   sockwrite.l=0                         ;Clear Writemask
  31.   sockwrite.l BitSet sock.l             ;set Writemask on our socket
  32.   g=WaitSelect_(2,0,&sockwrite.l,0,0,0) ;Wait until server is ready to read our data
  33.   c=sendto_(sock.l,ad,size,0,host,sendtolen)             ;Send data to server
  34.  
  35. End Statement
  36.  
  37. .ConnectUDP
  38. Statement ConnectUDP{host$,port.w}
  39.   SHARED sock.l,host.sockaddrin,hostlen2.l
  40.   ;
  41.   ; Connect to host at specified port
  42.   ; Return true or False if Connection is made
  43.   ;
  44.  
  45.   sock.l=socket_(2,2,0)        ; open UDP socket
  46.  
  47.  
  48.   *a.hostent=gethostbyname_(host$)     ; set up destination address to send packets to
  49.  
  50.   ;Copy Details to our Sockaddrin structure
  51.  
  52.   bb=CopyMem_(*a.hostent\h_addr_list\ItemA,&host.sockaddrin\sin_addr,*a.hostent\h_length)
  53.  
  54.   host.sockaddrin\sin_port=port       ;Set port number
  55.   host.sockaddrin\sin_family=2        ;Set type to AT_INET
  56.   hostlen2.l=SizeOf.sockaddrin        ;Get length of structure sockaddrin
  57.  
  58. End Function
  59.  
  60.  
  61. ;****************************************
  62.  
  63. WbToScreen0
  64. Window 0,0,20,300,220,$1|$2|$4|$8|$400,"UDP Send",1,2
  65.  
  66. WindowOutput0
  67. WindowInput 0
  68.  
  69. ConnectUDP {Hostname,#PORT}   ; open socket and set up destination address
  70.  
  71. NPrint "    Type in data, and hit"
  72. NPrint "      return to send"
  73.  
  74. ypos=50
  75. xpos=10
  76.  
  77. ;****************************************
  78.  
  79. .Main
  80.  
  81. Repeat
  82.     Delay_(1)
  83.     b$=Inkey$
  84.     If b$<>""
  85.        If b$=Chr$(13)       ;send string on return key
  86.          WLocate 10,30
  87.          Print "                                       "
  88.          xpos=10
  89.  
  90.          t$="SENT: " + send$
  91.          Gosub Print_String
  92.  
  93.          WriteUDP{&send$,Len(send$),hostlen2.l}    ; send string
  94.          send$=""
  95.        Else                 ; build string to send
  96.          send$=send$+b$
  97.          WLocate xpos,30
  98.          xpos+8
  99.          Print b$          ; echo key press
  100.        EndIf
  101.     EndIf
  102.  
  103.     ev.l=Event
  104.  
  105. Until ev=$200    ; shut down if close gadget hit
  106.  
  107. CloseTCP{}                          ; close connection
  108.  
  109. WLocate 10,30
  110. Print "                                       "
  111. WLocate 10,30
  112. Print"Connection  closed"
  113. Delay_(10)
  114.  
  115. FreeMem TCPmem,$2000
  116.  
  117. End
  118.  
  119. ;****************************************
  120.  
  121. .Print_String
  122.       ypos+10
  123.       If ypos=200 Then ypos=50
  124.       WLocate 10,ypos
  125.       Print "                                             "
  126.       WLocate 10,ypos
  127.       Print t$               ; print string received
  128. Return
  129.  
  130.